package com.share.shiro.simple;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;

/**
 * @author caifan
 * @created 2021/8/22
 * @description:
 */
public class QuickStart {
    private static final transient Logger log = LoggerFactory.getLogger(QuickStart.class);

    /**
     * 使用最简单的方式方式配置用户 角色 权限到ini配置文件
     * @param args
     */
    public static void main(String[] args) {
        // 1.读取配置文件
        Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
        // 2. 获取安全管理器
        SecurityManager securityManager = factory.getInstance();

        SecurityUtils.setSecurityManager(securityManager);

        // 获取登录对象
        Subject subject = SecurityUtils.getSubject();

        Session session = subject.getSession();
        session.setAttribute("loginKey", "loginValue");
        String value = (String) session.getAttribute("loginKey");
        if (Objects.equals(value, "loginValue")) {
            log.info("查到正确的value[" +  value + "]");
        }

        // 登录用户 校验 角色 权限
        if (!subject.isAuthenticated()) {
            UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");
            token.setRememberMe(true);
            try {
                subject.login(token);
            } catch (UnknownAccountException uae) {
                log.info("There is no user with username of " + token.getPrincipal());
            } catch (IncorrectCredentialsException ice) {
                log.info("Password for account " + token.getPrincipal() + " was incorrect!");
            } catch (LockedAccountException lae) {
                log.info("The account for username " + token.getPrincipal() + " is locked.  " +
                        "Please contact your administrator to unlock it.");
            }
            // ... catch more exceptions here (maybe custom ones specific to your application?
            catch (AuthenticationException ae) {
                ae.printStackTrace();
                //unexpected condition?  error?
            }

            log.info("User [" + subject.getPrincipal() + "] logged in successfully.");

            // 角色测试
            if (subject.hasRole("schwartz")) {
                log.info("May the Schwartz be with you!");
            } else {
                log.info("Hello, mere mortal.");
            }

            // 权限校验
            if (subject.isPermitted("lightsaber:wield")) {
                log.info("You may use a lightsaber ring.  Use it wisely.");
            } else {
                log.info("Sorry, lightsaber rings are for schwartz masters only.");
            }

            //a (very powerful) Instance Level permission:
            if (subject.isPermitted("winnebago:drive:eagle5")) {
                log.info("You are permitted to 'drive' the winnebago with license plate (id) 'eagle5'.  " +
                        "Here are the keys - have fun!");
            } else {
                log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");
            }

            //登出
            subject.logout();

            System.exit(0);
        }
    }
}
